package activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import com.example.administrator.ci_edit.R;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import db.UserConfigDatabaseHelper;
import entity.UserConfig;
import model.Constants;
import okhttp3.Call;

import static db.UserConfigDatabaseHelper.getNameAndPass;

/**
 * Created by mary on 2017/7/11.
 */

public class Login2Activity extends AppCompatActivity {
    Editor editor_re_pass;
    Button button5;
    Button button3;
    private EditText username;
    private EditText password;
    private Button loginBtn;
    private String name;
    private String pass;
    private CheckBox checkBox;
    private SharedPreferences sharedPreferences;
    // 返回的数据
    private String info;

    //接收数据的url
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.login2);
        checkBox = (CheckBox) findViewById(R.id.checkBox);
        username = (EditText) findViewById(R.id.name);
        password = (EditText) findViewById(R.id.password);
        loginBtn = (Button) findViewById(R.id.btn_login);

        if (checkBox.isChecked()) {
//            sharedPreferences=getSharedPreferences("info",MODE_PRIVATE);
//            editor_re_pass=sharedPreferences.edit();
//            editor_re_pass.putString("name",name);
//            editor_re_pass.putString("password",pass);
//            editor_re_pass.commit();
//
        }
        button5 = (Button) findViewById(R.id.button5);
        button5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setClass(Login2Activity.this, RegisteRActivity.class);
                startActivity(intent);
            }
        });
        button3 = (Button) findViewById(R.id.button3);
        button3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent1 = new Intent();
                intent1.setClass(Login2Activity.this, ForgetPasswordActivity.class);
                startActivity(intent1);
            }
        });


        loginBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                name = username.getText().toString();
                pass = password.getText().toString();

                if (null == name || name.length() <= 0 || pass == null || pass.length() <= 0) {
                    username.requestFocus();
                    Toast.makeText(Login2Activity.this, "账号或密码不能为空", Toast.LENGTH_SHORT).show();
                }

                // 儿子账户 test1
                if (name.equals(Constants.ERZI_ID) && pass.equals(Constants.ERZI_PSW)) {
                    UserConfig uc = new UserConfig();
                    uc.setUsername(name);
                    String huanying = UserConfigDatabaseHelper.getNameAndPass(name, pass);
                    Toast.makeText(getApplicationContext(), huanying, Toast.LENGTH_LONG).show();
                    Intent intent = new Intent(Login2Activity.this, XiaobaiAndDashen.class);
                    startActivity(intent);
                }
                //系统账户  test2
                if (name.equals(Constants.BABA_ID) && pass.equals(Constants.BABA_PSW)) {
                    UserConfig uc = new UserConfig();
                    uc.setUsername(name);
                    String huanying = UserConfigDatabaseHelper.getNameAndPass(name, pass);
                    Toast.makeText(getApplicationContext(), huanying, Toast.LENGTH_LONG).show();
                    Intent intent = new Intent(Login2Activity.this, XiaobaiAndDashen.class);
                    startActivity(intent);
                }

                //系统账户  test3
                if (name.equals("lxl") && pass.equals("erzi")) {
                    UserConfig uc = new UserConfig();
                    uc.setUsername(name);
                    String huanying = UserConfigDatabaseHelper.getNameAndPass(name, pass);
                    Toast.makeText(getApplicationContext(), huanying, Toast.LENGTH_LONG).show();
                    Intent intent = new Intent(Login2Activity.this, XiaobaiAndDashen.class);
                    startActivity(intent);
                }

                //1000个测试账户
                for (int i = 0; i < 1000; i++) {
                    String test = "" + i;
                    if (name.equals(test) && pass.equals(test)) {
                        UserConfig uc = new UserConfig();
                        uc.setUsername(name);
                        String huanying = UserConfigDatabaseHelper.getNameAndPass(name, pass);
                        Toast.makeText(getApplicationContext(), huanying, Toast.LENGTH_LONG).show();
                        Intent intent = new Intent(Login2Activity.this, XiaobaiAndDashen.class);
                        startActivity(intent);
                    }
                }


                OkHttpUtils.post()
                        .url(Constants.SERVER_URL + "login")
                        .addParams("username", name)
                        .addParams("password", pass)
                        .build()
                        .execute(new StringCallback() {
                            @Override
                            public void onError(Call call, Exception e, int id) {

                            }

                            @Override
                            public void onResponse(String response, int id) {
                                Toast.makeText(Login2Activity.this, "sda" + Integer.toString(id), Toast.LENGTH_SHORT).show();
                                if (response.equals("LOGIN_SUCCESS")) {
                                    UserConfig uc = new UserConfig();
                                    uc.setUsername(name);
                                    String huanying = UserConfigDatabaseHelper.getNameAndPass(name, pass);
                                    Toast.makeText(getApplicationContext(), huanying, Toast.LENGTH_LONG).show();
                                    Intent intent = new Intent(Login2Activity.this, XiaobaiAndDashen.class);
                                    startActivity(intent);
                                }
                            }
                        });
            }
        });
    }
}
